package page.Travel;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import app.WebApp;
import entity.Travel;
import dao.TravelDao;
import dao.UserDao;
import entity.User;


public class UserPage extends WebPage {

	public UserPage(final User user) {
		super();

		// create FORM component which required to POST data from client->server
		final Form<User> form = new Form<User>("form", new CompoundPropertyModel<User>(user));
		add(form);

		// add input fields to form
		form.add(new TextField<String>("price"));
		form.add(new TextField<String>("type"));

		// add 'save' button
		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				// retrieve instance of DAO
				final UserDao dao = new UserDao(WebApp.DATA_XML_FOLDER);
				// is new Circle?
				if (user.getId() != null) {
					dao.saveNew(user);
				} else {
					// execute update of the new object
					dao.update(user);
				}
				// go to the list page
				setResponsePage(new TravelListPage());
			}
		});

		// add 'cancel' button
		form.add(new Link("cancel") {
			@Override
			public void onClick() {
				// go back to the list page
				setResponsePage(new TravelListPage());
			}
		});
	}
}